﻿using System.Reflection;

namespace KinonekoSoftware.Foundation.Languages
{
    public class AssemblyLanguageProvider : LanguageProvider
    {
        private readonly Assembly _IncludeResourceAssembly;
        private readonly string   _IncludeResourceFolderName;

        protected AssemblyLanguageProvider(Type thisLanguageProviderInAssembly, string folderName)
        {
            var t = thisLanguageProviderInAssembly ?? throw new ArgumentNullException(nameof(thisLanguageProviderInAssembly));
            _IncludeResourceAssembly   = Assembly.GetAssembly(t);
            _IncludeResourceFolderName = folderName;
        }

        
        
        protected virtual IReadOnlyList<string> GetResourceManifestStreamByName(LanguageArea language)
        {
            var fileName = GetDefaultFileName(language);
            var path     = $"{_IncludeResourceFolderName}.{fileName}";
            var stream   = _IncludeResourceAssembly.GetManifestResourceStream(path);
            var list     = new List<string>(128);

            if (stream is null)
            {
                return Array.Empty<string>();
            }

            using (var sr = new StreamReader(stream))
            {
                while (!sr.EndOfStream)
                {
                    var line = sr.ReadLine();
                    if(!string.IsNullOrEmpty(line))
                        list.Add(line);
                }
            }

            return list;
        }

        public override IReadOnlyList<string> GetUnformattedLines(LanguageArea language)
        {
            var lists = GetResourceManifestStreamByName(language);
            return lists;
        }
    }
}